<?php

namespace Admin\Controller;

use Think\Controller;
use Org\Util;

/**
 * Description of DemoController
 *
 * @author YourName
 */
class ElectricityController extends AdminController {
	
	/**
	 * 列表
	 * 
	 * @author YourName <YourEmail>
	 */
	public function index() {
		// $name = I('name');
		// $map['name'] = array('like', '%' . (string) $name . '%');
		// $list = $this->lists('electricity', $map);
		// int_to_string($list);
		// $this->assign('_list', $list);
		// $this->meta_title = '列表页标题';
		$this->display ();
	}

	public function monthEx() {
		$this->assign('type', "1");
		$this->month();
	}
	public function dailyEx() {
		$this->assign('type', "1");
		$this->daily();	
	}
	public function weeklyEx() {
		$this->assign('type', "1");
		$this->weekly();	
	}
	public function month() {
		
		$date = I( 'date' );
		if (empty($date)) {
			$result = D()->query("select max(start_date) monthDate from month_report");
			$date = $result[0]["monthDate"];
		}else{
			$date = $date . "-01";
		}
		if(empty($date)){
			$date = date("Y-m-01");
		}

		$this->assign ( 'date', $date );
		$this->display ("month");
	}
	public function daily() {
		$date = I ( 'date' );
		if (empty($date)) {
			$result = D()->query("select max(daily_date) dailyDate from daily_report");
			$date = $result[0]["dailyDate"];
		}
		if(empty($date)){
			$date = date("Y-m-d");
		}
		$this->assign ( 'date', $date );
		$this->display ("daily");
	}
	public function weekly() {
		$flag = 1;
		$date = I ( 'date' );
		if (empty($date)) {
			$result = D()->query("select max(start_date) weekDate from weekly_report");
			$date = $result[0]["weekDate"];
			$flag = 0;
		}
		if(empty($date)){
			$date = date("Y-m-d");
		}
		// 获取星期一的天日期
		if($flag==1){
			$time = strtotime($date);
			$week = date('N', $time);
			if($week!=1){
				$date = date('Y-m-d',strtotime('-1 monday', $time));
			}
		}
		$this->assign ( 'date', $date );
		$this->display ("weekly");
	}

	public function real() {
        $name = I('name');
        $map['meter_name'] = array('like', '%' . (string) $name . '%');
        $list = $this->lists('real_time_electricity', $map);
        int_to_string($list);
        $this->assign('_list', $list);
        $this->meta_title = '列表页标题';
        $this->display();
	}
	
	public function real_time_electricity() {
// 		$date = I ( 'date' );
// 		$map ['start_date'] = $date;
		$list = $this->lists ( 'real_time_electricity');
		int_to_string ( $list );
		$this->ajaxReturn ( $list, 'JSON' );
	}
	public function month_report() {
		$date = I ( 'date' );
		$map ['start_date'] = $date;
		$list = $this->lists ( 'month_report',$map );
		int_to_string ( $list );
		$this->ajaxReturn ( $list, 'JSON' );
	}
	public function weekly_report() {
		$date = I ( 'date' );
		$map['start_date'] = $date;
		$list = $this->lists ( 'weekly_report',$map );
		int_to_string ( $list );
		$this->ajaxReturn ( $list, 'JSON' );
	}
	public function daily_report() {
		$date = I ( 'date' );
		$map['daily_date'] = $date;
		$list = $this->lists ( 'daily_report', $map );
		int_to_string ( $list );
		$this->ajaxReturn ( $list, 'JSON' );
	}
	
	/**
	 * 添加
	 * 
	 * @author YourName <YourEmail>
	 */
	public function add($id = '', $meter_id = '', $read = '', $type_a = '', $type_b = '', $type_c = '', $collection_time = '') {
		if (IS_POST) {
			$electricity = array (
					'id' => $id,
					'meter_id' => $meter_id,
					'read' => $read,
					'type_a' => $type_a,
					'type_b' => $type_b,
					'type_c' => $type_c,
					'collection_time' => $collection_time 
			)
			;
			$id = M ( 'electricity' )->add ( $electricity );
			if (! $id) {
				$this->error ( '添加失败！' );
			} else {
				$this->success ( '添加成功！', U ( 'index' ) );
			}
			;
		} else {
			$this->display ();
		}
	}
	
	/**
	 * 编辑
	 * 
	 * @author YourName <YourEmail>
	 */
	public function edit($id = '', $meter_id = '', $read = '', $type_a = '', $type_b = '', $type_c = '', $collection_time = '') {
		if (IS_POST) {
			$electricity = array (
					'id' => $id,
					'meter_id' => $meter_id,
					'read' => $read,
					'type_a' => $type_a,
					'type_b' => $type_b,
					'type_c' => $type_c,
					'collection_time' => $collection_time 
			)
			;
			if (! M ( 'electricity' )->where ( array (
					'id' => $id 
			) )->save ( $electricity )) {
				$this->error ( '修改失败！' );
			} else {
				$this->success ( '修改成功！', U ( 'index' ) );
			}
			;
		} else {
			$data = M ( 'electricity' )->where ( array (
					'id' => $id 
			) )->find ();
			$this->data = $data;
			$this->display ();
		}
	}
	
	/**
	 * 删除方法
	 * 
	 * @author YourName <YourEmail>
	 */
	public function del($id = '') {
		if (! is_null ( $id )) {
			if (! M ( 'electricity' )->where ( array (
					'id' => $id 
			) )->delete ()) {
				$this->error ( '删除失败！' );
			} else {
				$this->success ( '删除成功！', U ( 'index' ) );
			}
			;
		}
	}
	
	/**
	 * 状态修改
	 * 
	 * @author YourName <YourEmail>
	 */
	public function changeStatus($method = null, $id) {
		$id = is_array ( $id ) ? implode ( ',', $id ) : $id;
		if (empty ( $id )) {
			$this->error ( '请选择要操作的数据!' );
		}
		$map ['id'] = array (
				'in',
				$id 
		);
		switch (strtolower ( $method )) {
			case 'forbid' :
				$this->forbid ( 'electricity', $map );
				break;
			case 'resume' :
				$this->resume ( 'electricity', $map );
				break;
			case 'delete' :
				$this->delete ( 'electricity', $map );
				break;
			default :
				$this->error ( 'electricity' );
		}
	}
}